<template>
  <el-dialog :model-value="dialogData.isShow" :title="dialogData.type == 'edit'?'编辑':'新增'"
             @close="closeDialog" draggable>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="专家分类名称" prop="categoryName">
            <el-input v-model="form.categoryName" maxlength="255" placeholder="请输入专家分类名称"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="上级" prop="description">
            <custom-tree-select v-model:value="form.parentId" :options="expertCategoryList" placeholder="请选择上级菜单"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input type="number" v-model="form.sort" maxlength="255" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, FormInstance, FormRules} from "element-plus";
import {addExpertCategory, getExpertCategory, updateExpertCategory, getExpertCategoryList} from "@/api/app/expertCategory";

const formRef = ref<FormInstance>()

const emits = defineEmits<{
  (event: 'refresh'): void
}>()

// 表单
let form: any = ref({
  id: null,
  categoryName: null,
  sort: null,
  parentId: null,
});

// 效验规则
const rules = reactive<FormRules>({})

// 获取详情
const getDetails = (id: string) => {
  getExpertCategory(id).then(res => {
    form.value = Object.assign({}, form.value, res);
  })
}

// 弹框数据
const dialogData = reactive({
  isShow: false,
  id: null,
  type: '',
  parentId: null,
})

// 打开弹框
const openDialog = async (props: any) => {
  const { type, id, parentId } = props
  dialogData.isShow = true;
  dialogData.title = '新增专家';
  dialogData.type = type
  dialogData.parentId = id
  if (type == 'edit') {
    dialogData.id = id;
    dialogData.parentId = parentId
    dialogData.title = '编辑专家';
    getDetails(id);
  }
  getAllExpertCategory()
}

// 关闭弹框
const closeDialog = () => {
  dialogData.isShow = false;
  dialogData.id = null;
  form.value = {};
}

// 提交
const submit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate((valid: any) => {
    if (valid) {
      let data = form.value;
      if (data.id) {
        updateExpertCategory({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      } else {
        addExpertCategory({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      }
    }
  })
}

const expertCategoryList = ref([])

const getAllExpertCategory = () => {
  getExpertCategoryList({}).then(res => {
    let data = res || []
    function fun(dataList: any) {
      dataList.map((item: any) => {
        if (item.children) fun.call(this, item.children)
        Object.assign(item, {name: item.categoryName})
      })
    }
    fun.call(this, data)
    expertCategoryList.value = [{
      id: '0',
      name: '根目录',
      children: data
    }]
    form.value.parentId = dialogData.parentId
  })
}

defineExpose({
  openDialog,
});
</script>

<style scoped>

</style>
